package net.wanho.systemSettings.controller;

import net.wanho.commons.servlet.BaseServlet;
import net.wanho.systemSettings.po.Menu;
import net.wanho.systemSettings.service.MenuService;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/MenuServlet")
public class MenuServlet extends BaseServlet<MenuService> {
	protected String getAllMenu(HttpServletRequest request,HttpServletResponse response){
			String name = request.getParameter("name");
			Map<String,String> condMap = new HashMap<>(1);
			if (StringUtils.isNotEmpty(name)){
				condMap.put("name",name) ;
			}
			List<Menu> parentMenuList = super.baseService.findByCond(condMap);
			request.setAttribute("parentMenuList",parentMenuList);
			return "/WEB-INF/view/systemSettings/menu/index.jsp";
	}
	
	protected void  getAllParentMenu(HttpServletRequest request,HttpServletResponse response){
			try {
				request.getRequestDispatcher("/WEB-INF/view/systemSettings/menu/create.jsp").forward(request,response);
			} catch (ServletException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		
	}
	
	protected void addMenu(HttpServletRequest request,HttpServletResponse response){
			try {
				response.sendRedirect(request.getContextPath()+ "/MenuServlet?method=getAllMenu");
			} catch (IOException e) {
				e.printStackTrace();
			}
	}
	
	protected void deleteMenuById(HttpServletRequest request,HttpServletResponse response){
		try {
			response.sendRedirect(request.getContextPath()+ "/MenuServlet?method=getAllMenu");
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	protected void getUpdateMenudetailById(HttpServletRequest request,HttpServletResponse response){
		try {
			request.getRequestDispatcher("/WEB-INF/view/systemSettings/menu/update.jsp").forward(request,response);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ServletException e) {
			e.printStackTrace();
		}
	}
	
	protected void updateMenu(HttpServletRequest request,HttpServletResponse response){
		try {
			response.sendRedirect(request.getContextPath()+ "/MenuServlet?method=getAllMenu");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}
